package com.sp2.dev.servlet;

import com.sp2.dev.constant.JDBCPoperties;
import com.sp2.dev.constant.MessageProperties;
import com.sp2.dev.util.SystemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-1-23
 * Time: 上午10:10
 * To change this template use File | Settings | File Templates.
 */
public class SystemService {

    private static Logger logger = LoggerFactory.getLogger(SystemService.class);

    private static SystemUtil util = new SystemUtil();

    /**
     * 加载常用的信息
     *
     * @throws IOException
     */
    public void loadMessageProperties() throws IOException {
        String messagePath = util.getMetaInfFolderPath();
        String file = messagePath + "/message.properties";
        InputStream io = null;
        try {
            io = new FileInputStream(new File(file));
            Properties prop = new Properties();
            prop.load(io);
            MessageProperties.test_message = prop.getProperty("test_message");
            MessageProperties.save_cost_time = prop.getProperty("save_cost_time");
            MessageProperties.save_file = prop.getProperty("save_file");
            logger.info("-------------loadMessageProperties----------------");
        } catch (IOException e) {
            logger.error("loadMessageProperties e" + e + file);
        } finally {
            if (io != null) {
                io.close();
            }
        }
    }

    /**
     * 加载jdbc链接
     */
    public void loadJDBCProperties() throws IOException {
        String jdbcPath = util.getMetaInfFolderPath();
        String file = jdbcPath + "/jdbc.properties";
        InputStream io = null;
        try {
            io = new FileInputStream(new File(file));
            Properties prop = new Properties();
            prop.load(io);
            JDBCPoperties.jdbc_driver = prop.getProperty("jdbc.driver");
            JDBCPoperties.jdbc_url = prop.getProperty("jdbc.url");
            JDBCPoperties.jdbc_user = prop.getProperty("jdbc.user");
            JDBCPoperties.jdbc_pass = prop.getProperty("jdbc.pass");
            logger.info("-------------loadJDBCProperties----------------");
        } catch (Exception e) {
            logger.error("loadJDBCProperties e" + e + file);
        } finally {
            if (io != null) {
                io.close();
            }
        }

    }
}
